package notice

import (
	"errors"

	"github.com/BurntSushi/toml"
)

var smsNotice *SmsNoticer
var edmNoticer *EdmNoticer
var weixinNotice *WeiXinNoticer
var noticeConfig NoticeConfig
var ErrNotConfiged = errors.New("no config for notice")

func LoadConfig(path string) error {
	_, err := toml.DecodeFile(path, &noticeConfig)
	if err != nil {
		return err
	}
	smsNotice = NewSmsNoticer(&noticeConfig.Notice.SmsConfig)
	edmNoticer = NewEdmNoticer(&noticeConfig.Notice.EdmConfig)
	weixinNotice = NewWeiXinNoticer(&noticeConfig.Notice.WeiXinConfig)
	return nil
}

func SendSms(number, content string) (string, error) {
	if smsNotice == nil {
		return "", ErrNotConfiged
	}
	return smsNotice.Send(number, content)
}

func SendSmsMerge(key, number, content string) {
	if smsNotice == nil {
		return
	}
	smsNotice.SendMerge(key, number, content)
}

func SendEmail(systemType, recipient, subject, content string) (string, error) {
	if edmNoticer == nil {
		return "", ErrNotConfiged
	}
	return edmNoticer.Send(systemType, recipient, subject, content)
}

func SendEmailMerge(key, systemType, recipient, subject, content string) {
	if edmNoticer == nil {
		return
	}
	edmNoticer.SendMerge(key, systemType, recipient, subject, content)
}

func SendWeiXin(id, message string) (string, error) {
	if weixinNotice == nil {
		return "", ErrNotConfiged
	}
	return weixinNotice.Send(id, message)
}

func SendWeiXinMerge(key, id, message string) {
	if weixinNotice == nil {
		return
	}
	weixinNotice.SendMerge(key, id, message)
}
